#________________________________________________________________________________________________
# 1. extract key vars from epw file
# 2. generate cumulative diffuse radiation across the sky vault via GenCumulativeSky.com
# Ji Zhang @ Apr 2012

import os
import csv

#________________________________________________________________________________________________
# open epw file to read
epw_file = "./sg.epw"
f_epw    = open(epw_file, "r")
data     = csv.reader(f_epw, delimiter=',')

#________________________________________________________________________________________________
#get the absolute path of the epw file
epw_file_abspath = os.path.abspath(epw_file)                                  
#print epw_file_abspath

#get the directory of the epw file
epw_file_dirname = os.path.dirname(epw_file_abspath)
#print epw_file_dirname

#________________________________________________________________________________________________
# write header of results file

f_epw_key_vars = open("./epw_key_vars.csv", "w")

f_epw_key_vars.write("year, month, day, hour, minute," +\
                     "global_horizontal_radiation," +\
                     "direct_normal_radiation," +\
                     "diffuse_horizontal_radiation," +\
                     "global_horizontal_illuminance," +\
                     "direct_normal_illuminance," +\
                     "diffuse_horizontal_illuminance," +\
                     "zenith_luminance" + "\n")
f_epw_key_vars.close()

#________________________________________________________________________________________________
f_epw_key_vars = open("epw_key_vars.csv", "a")

# read and write out location infor and key vars
counter = 0
for row in data:
    if counter == 0:
        city_name = row[1]
        lat = float(row[6])
        lon = float(row[7])
        tz  = float(row[8])
        sdm = tz*15
        
        if lon > 0:
            ori_to_greenwich = "East of Greenwich"
        elif lon < 0:
            ori_to_greenwich = "West of Greenwich"
        else:
            ori_to_greenwich = "Greenwich"
    else:
        pass

    if counter >= 8:
        year  = int(row[0])
        month = int(row[1])
        day   = int(row[2])
        hour  = int(row[3])
        minute= int(row[4])

        g_h_rad = float(row[13])
        d_n_rad = float(row[14])
        d_h_rad = float(row[15])

        g_h_illu = float(row[16])
        d_n_illu = float(row[17])
        d_h_illu = float(row[18])

        zen_lumi = float(row[19])

        f_epw_key_vars.write(str(year)     + "," +\
                             str(month)    + "," +\
                             str(day)      + "," +\
                             str(hour)     + "," +\
                             str(minute)   + "," +\
                             str(g_h_rad)  + "," +\
                             str(d_n_rad)  + "," +\
                             str(d_h_rad)  + "," +\
                             str(g_h_illu) + "," +\
                             str(d_n_illu) + "," +\
                             str(d_h_illu) + "," +\
                             str(zen_lumi) + "\n")
    else:
        pass

    counter += 1

f_epw_key_vars.close()  
f_epw.close()

#________________________________________________________________________________________________
print "city name __________", city_name
print "latitude ___________", lat
print "longitude __________", lon, ori_to_greenwich
print "time zone __________", tz
print "standard meridian __", sdm

#________________________________________________________________________________________________
# rename results file by city name
epw_key_var_file_abspath = epw_file_dirname + "/epw_key_vars.csv"
#print epw_key_var_file_abspath

epw_key_var_file_new     = epw_file_dirname + "/epw_key_vars_" + city_name + ".csv"
#print epw_key_var_file_new

epw_key_var_file_new_exist_or_not = os.path.exists(epw_key_var_file_new)

if epw_key_var_file_new_exist_or_not :
    os.remove(epw_key_var_file_new)
else:
    pass

os.rename(epw_key_var_file_abspath, epw_key_var_file_new)

#________________________________________________________________________________________________
# create autorun.bat

f_autorun = open("./autorun.bat", "w")

#write starting time
f_autorun.write("@echo start at : %date% %time%" + "\n\n")

#set path
f_autorun.write("SET RAYPATH=.;C:\DIVA\Radiance\lib;C:\DIVA\Radiance\\bin;C:\DIVA\DaysimBinaries;$RAYPATH" +"\n\n")
f_autorun.write("SET PATH=.;C:\DIVA\Radiance\lib;C:\DIVA\Radiance\\bin;C:\DIVA\DaysimBinaries;$PATH" +"\n\n")

###write gencumulativesky cmd with the -r option (radiance value is divided by 179000)
f_autorun.write("GenCumulativeSky +s1 " +\
                " -a " + str(lat) +\
                " -o " + str(lon*(-1)) +\
                " -m " + str(int(sdm*(-1))) +\
                " -r -E -time 0 24 -date 1 1 12 31 " +\
                epw_file +\
                " >  ./z_output___gcs_vis.cal  2>./log_err.log" + "\n\n")

#write gencumulativesky cmd with the -p option (radiance value is divided by 1000)
##f_autorun.write("GenCumulativeSky +s1 " +\
##                " -a " + str(lat) +\
##                " -o " + str(lon*(-1)) +\
##                " -m " + str(int(sdm*(-1))) +\
##                " -p -E -time 0 24 -date 1 1 12 31 " +\
##                epw_file +\
##                " >  ./z_output___gcs_vis.cal  2>./log_err.log" + "\n\n")

#write gencumulativesky cmd without the -r and -p option
##f_autorun.write("GenCumulativeSky +s1 " +\
##                " -a " + str(lat) +\
##                " -o " + str(lon*(-1)) +\
##                " -m " + str(int(sdm*(-1))) +\
##                " -E -time 0 24 -date 1 1 12 31 " +\
##                epw_file +\
##                " >  ./z_output___gcs_vis.cal  2>./log_err.log" + "\n\n")

#writ oconv cmd
f_autorun.write("oconv -f  ./gcs_vis_gcsky.rad  >  ./z_output___gcs_vis.oct  2>>./log_err.log" + "\n\n")

#write rpict cmd
f_autorun.write("rpict -t 15 -i -ab 1 -ad 1000 -as 20 -ar 300 -aa 0.1 " +\
                "-vts -vp 0 0 0 -vd 0 0 1 -vu 0 1 0 -vh 180 -vv 180 -x 1024 -y 1024 " +\
                "./z_output___gcs_vis.oct " +\
                " > " +\
                "./z_output___gcs_vis_Perspective_kwhm-2.pic " +\
                "2>>./log_err.log" + "\n\n")

#write falsecolor2 cmd
f_autorun.write("falsecolor2 -i ./z_output___gcs_vis_Perspective_kwhm-2.pic " +\
                "-s auto -n 10 -l kWhm-2 -z " +\
                " > " +\
                "./z_output___gcs_vis_Perspective_fc.pic " +\
                "2>>./log_err.log" + "\n\n")

#write ending time
f_autorun.write("@echo." + "\n\n")
f_autorun.write("@echo end at : %date% %time%")

f_autorun.close()

#execute autorun.bat and cmd history is saved to log file
autorun_str = "autorun.bat > ./log_cmd.log"
os.system(autorun_str)

